package zym.diandian.module.sys;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import zym.diandian.R;

/**
 * Created by Elio on 2016/5/7.
 */
public class SysinfoAdapter extends BaseAdapter {
    private List<String> mList = null;
    private Context mContext;
    private int item_height;

    public SysinfoAdapter(Context context, List<String> data, int item_height) {
        this.mContext = context;
        if (data == null) {
            data = new ArrayList<>();
            data.add("@正在加载,请稍后...");
        }
        this.mList = data;
        this.item_height = item_height;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return this.mList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return this.mList.get(position);
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return !(mList.get(position).startsWith("@"));
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        int type;
        if (!isEnabled(position)) {
            type = 0;// 加载提示 和 内容分割
        } else {
            type = 1;
        }
        return type;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            ViewHolder holder;
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, item_height * 2);
            if (null == convertView) {
                holder = new ViewHolder();
                this.isEnabled(position);
                switch (this.getItemViewType(position)) {
                    case 0:
                        convertView = View.inflate(mContext, R.layout.info_item_load, null);
                        holder.tv_1 = (TextView) convertView.findViewById(R.id.item_left_load);
                        break;
                    case 1:
                        convertView = View.inflate(mContext, R.layout.info_item, null);
                        holder.tv_1 = (TextView) convertView.findViewById(R.id.item_left);
                        holder.tv_2 = (TextView) convertView.findViewById(R.id.item_right);
                        break;
                    default:
                        break;
                }
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            switch (this.getItemViewType(position)) {
                case 0:
                    String separators = this.mList.get(position).replace("@", "");
                    holder.tv_1.setText("" + separators);
                    break;
                case 1:
                    try {
                        String[] d = this.mList.get(position).split("@");
                        holder.tv_1.setText(d[0]);
                        holder.tv_2.setText(d[1]);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }

            convertView.setLayoutParams(lp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    private class ViewHolder {

        TextView tv_1;

        TextView tv_2;
    }

}
